//
//  SSTabBarView.swift
//  Weibo项目
//
//  Created by vn on 16/7/19.
//  Copyright © 2016年 vn. All rights reserved.
//

import UIKit

class SSTabBarView: UITabBar {
    
    var closure:(()->())?
    
  
  override init(frame: CGRect) {
        
        super.init(frame: frame)
        
    
        setUpAddButton()
    
        
        
    }

  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
    
    
    //设置描述+按钮
    func setUpAddButton(){
        
        addSubview(add_btn)
        
         
    }
    
    
    //给addButton设置frame
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = frame.width * 0.2
        var index:CGFloat = 0
        
        
        
        for value in subviews{
            
            // 判断value 是否是UITabBarButton
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                // 设置UITabBarButton frame
                value.frame.origin.x = index * w
                value.frame.size.width = w
                
                index++
                
                //如果是第二个需要在加一下
                if index == 2 {
                    index++
                }
            }
            
        }
        
        add_btn.center.x = frame.width * 0.5
        add_btn.center.y = frame.height * 0.5
        
    }
    
    //添加监听方法
    func addButtonClick(){
        
        closure?()
        
    }
    
    lazy var add_btn:UIButton = {
        
       let btn = UIButton()
        
        //添加监听方法
        btn .addTarget(self, action: "addButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn .setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.sizeToFit()
        
        return btn
        
    }()
    
    
}
